package com.walker.utils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class DateParser {
    public static Map<Integer, String> generateDateMap(String input) {
        Map<Integer, String> result = new HashMap<>();

        // 去除 ${} 并提取日期和偏移部分
        String cleanInput = input.replace("${", "").replace("}", "");
        String[] parts = cleanInput.split("[+-]");
        String dateStr = parts[0];
        int offset = parts.length > 1 ? Integer.parseInt(parts[1]) : 0;
        boolean isNegative = cleanInput.contains("-");

        // 解析日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate date = LocalDate.parse(dateStr, formatter);

        // 生成 Map
        int keyCount = offset == 0 ? 0 : offset;
        for (int i = 0; i <= keyCount; i++) {
            LocalDate currentDate = isNegative ? date.minusDays(i) : date.plusDays(i);
            String formattedDate = "/" + currentDate.format(formatter);
            result.put(i, formattedDate);
        }

        // 如果没有偏移（即 ${20250421} 格式），只添加当天
        if (offset == 0) {
            result.put(0, "/" + dateStr);
        }

        return result;
    }

    // 测试方法
    public static void main(String[] args) {
        System.out.println(generateDateMap("${20250401-4}")); // {0=/20250422, 1=/20250421, 2=/20250420}
        System.out.println(generateDateMap("${20240422-2}")); // {0=/20250423, 1=/20250424}
    }
}